package com.mall.order.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.commons.tool.exception.ValidateException;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dal.entitys.*;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import com.mall.order.utils.ExceptionProcessorUtils;
import com.mall.user.IAddressService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * ciggar
 * create-date: 2019/7/30-上午10:04
 */
@Slf4j
@Component
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderConverter orderConverter;

    @Override
    public OrderDetailResponse queryOrdersById(OrderDetailRequest request) {

        OrderDetailResponse resp = new OrderDetailResponse();

        try {
            request.requestCheck();

            String orderId = request.getOrderId();

            Order order = orderMapper.selectByPrimaryKey(orderId);

            Integer orderStatus = order.getStatus();
            Long userId = order.getUserId();

            // 订单总金额
            BigDecimal orderTotal = new BigDecimal(0);

            // 根据用户ID查找tb_order_shipping
            OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
            String userName = orderShipping.getReceiverName();
            String tel = orderShipping.getReceiverPhone();
            String streetName = orderShipping.getReceiverAddress();

            // 通过订单Id获取所购商品列表
            Example example = new Example(OrderItem.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("orderId", orderId);
            List<OrderItem> orderItems = orderItemMapper.selectByExample(example);
            for (OrderItem orderItem : orderItems) {
                BigDecimal totalFee = orderItem.getTotalFee();
                orderTotal = orderTotal.add(totalFee);
            }
            List<OrderItemDto> goodsList = orderConverter.item2dto(orderItems);

            resp.setGoodsList(goodsList);
            resp.setOrderStatus(orderStatus);
            resp.setOrderTotal(orderTotal);
            resp.setStreetName(streetName);
            resp.setTel(tel);
            resp.setUserId(userId);
            resp.setUserName(userName);
            resp.setCode(OrderRetCode.SUCCESS.getCode());
            resp.setMsg(OrderRetCode.SUCCESS.getMessage());
        } catch (ValidateException validateException) {
            resp.setCode(OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getCode());
            resp.setMsg(OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        } catch (Exception e) {
            resp.setCode(OrderRetCode.SYSTEM_ERROR.getCode());
            resp.setMsg(OrderRetCode.SYSTEM_ERROR.getMessage());
        }
        return resp;
    }
}
